package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPackersBinding;
import com.sgb.kjwl.model.entity.PackersBusinessScopeEntity;
import com.sgb.kjwl.model.entity.PackersEntity;
import com.sgb.kjwl.utils.NumberConstant;
import com.sgb.kjwl.viewmodel.packers.PackersModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.DialogUtil;
import com.swgk.core.util.MDateUtil;

import java.util.Locale;
import java.util.Objects;

import javax.inject.Inject;

/**
 * @author pangyuheng
 * @date 2019/7/10
 * description:工队详情
 */
public class PackersActivity extends AppActivity{
    private ActivityPackersBinding binding;
    private String mobile = "";
    private String name = "";
    @Inject
    PackersModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static void start(Context context, String id) {
        Intent intent = new Intent();
        intent.putExtra("id", id);
        intent.setClass(context, PackersActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        binding = DataBindingUtil.setContentView(PackersActivity.this, R.layout.activity_packers);
        binding.setViewModel(viewModel);
    }

    @Override
    protected void init() {
        String id = getIntent().getStringExtra("id");
        viewModel.getInfo(id);

        //业绩介绍
        openAndCloseText(binding.tvAchevementIntroduction, binding.tvOpenAchevementIntroduction);

        //工队介绍
        openAndCloseText(binding.tvPackersIntroduction, binding.tvOpenPackersIntroduction);

        //设备介绍
        openAndCloseText(binding.tvDeviceIntroduction, binding.tvOpenDeviceIntroduction);

        binding.tvKefuMobile.setOnClickListener(this);
        binding.tvPersonMobile.setOnClickListener(this);
        binding.ivBack.setOnClickListener(this);
    }

    /**
     * 展开收起文本
     * @param text 展开收起的展示字符控件
     * @param button 展开收起的按钮字符控件
     */
    public void openAndCloseText(TextView text, TextView button){
        text.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int lineCount = text.getLineCount();
                if (lineCount > NumberConstant.NUMBER_THREE){
                    text.setMaxLines(NumberConstant.NUMBER_THREE);
                    button.setVisibility(View.VISIBLE);
                }else {
                    button.setVisibility(View.GONE);
                }
                text.getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getString(R.string.app_expand).contentEquals(button.getText())){
                    button.setText("收起");
                    text.setMaxLines(NumberConstant.NUMBER_TWENTY);
                    //判断是否少于5行
                    text.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                        @Override
                        public boolean onPreDraw() {
                            int lineCount = text.getLineCount();
                            if (lineCount <= NumberConstant.NUMBER_FIVE){
                                button.setVisibility(View.GONE);
                            }else {
                                button.setVisibility(View.VISIBLE);
                            }
                            text.getViewTreeObserver().removeOnPreDrawListener(this);
                            return false;
                        }
                    });

                }else {
                    button.setText("展开");
                    text.setMaxLines(NumberConstant.NUMBER_THREE);
                }
            }
        });
    }

    @Override
    public void onMultiClick(View v) {
        super.onMultiClick(v);
        int id = v.getId();
        if (id == R.id.tv_kefu_mobile) {
            DialogUtil.showTelDialog(this, "客服", "4000010051");
        } else if (id == R.id.tv_person_mobile) {
            if ("".equals(mobile)) {
                Toast.makeText(PackersActivity.this, "此联系人无号码，请咨询客服核实", Toast.LENGTH_SHORT).show();
            } else {
                DialogUtil.showTelDialog(this, name, mobile);
            }
        }
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
//        DaggerPackersComponent.builder().appComponent(appComponent).packersModule(new PackersModule(this)).build().inject(this);
    }

    public void showPackersBusinessScope(PackersBusinessScopeEntity businessScopeEntity){
        if (businessScopeEntity!=null && businessScopeEntity.getFirst()!=null && businessScopeEntity.getFirst().size()>0){
            binding.tvIndustryCategory.setText(businessScopeEntity.getFirst().get(0).getName());
        }

        //往容器内添加TextView数据
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 3, 10, 3);
        binding.flBusinessScope.removeAllViews();
        for (int i = 0; i < Objects.requireNonNull(businessScopeEntity).getSecond().size(); i++) {
            TextView tv = new TextView(this.getActivity());
            tv.setPadding(28, 5, 28, 5);
            tv.setText(businessScopeEntity.getSecond().get(i).getName());
            tv.setTextColor(ContextCompat.getColor(this,R.color.color_0286DF));
            tv.setMaxEms(10);
            tv.setSingleLine();
            tv.setBackgroundResource(R.drawable.corner_blue_shape);
            tv.setLayoutParams(layoutParams);
            binding.flBusinessScope.addView(tv, layoutParams);
        }
    }

    public void showPackersDetail(PackersEntity data) {
        if (data.getConcatPhone()!=null && !"".equals(data.getConcatPhone())){
            mobile = data.getConcatPhone();
        }
        if (data.getConcat()!=null && !"".equals(data.getConcat())){
            name = data.getConcat();
        }
        binding.tvProfession.setText(data.getTitle());
        binding.tvTime.setText(MDateUtil.getDate(String.valueOf(data.getSubmitTime())));

        binding.teamSize.setText(String.format(Locale.CHINA,"%d人",data.getScale()));
        binding.tvArea.setText(data.getLocationName());

        if (data.getTeamComposition()==null || "".equals(data.getTeamComposition())){
            binding.tvTeamComposition.setText("暂无介绍");
        }else {
            binding.tvTeamComposition.setText(data.getTeamComposition());
        }

        if (data.getIntroduce()==null || "".equals(data.getIntroduce())){
            binding.tvPackersIntroduction.setText("暂无介绍");
        }else {
            binding.tvPackersIntroduction.setText(data.getIntroduce());
        }

        if (data.getPerformance()==null || "".equals(data.getPerformance())){
            binding.tvAchevementIntroduction.setText("暂无介绍");
        }else {
            binding.tvAchevementIntroduction.setText(data.getPerformance());
        }
        if (data.getEquipment()==null || "".equals(data.getEquipment())){
            binding.tvDeviceIntroduction.setText("暂无介绍");
        }else {
            binding.tvDeviceIntroduction.setText(data.getEquipment());
        }

    }
}
